package com.sunyur.tailor.channel.service.inner.returnorder.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.returnorder.model.CreateReturnOrderModel;
import com.sunyur.tailor.channel.service.inner.returnorder.model.ReturnOrderHeadModel;
import com.sunyur.tailor.channel.service.inner.returnorder.model.ReturnOrderItemModel;
import com.sunyur.tailor.channel.service.inner.returnorder.param.CreateReturnOrderParam;
import com.sunyur.tailor.channel.service.inner.returnorder.param.ReturnOrderHeadParam;
import com.sunyur.tailor.channel.service.inner.returnorder.param.ReturnOrderItemParam;
import com.sunyur.tailor.channel.service.inner.returnorder.service.SunyurReturnOrderService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SunyurReceiveOrderServiceImpl
 * @Description 商越收货单服务
 * @Author yu.chen
 * @Date 2021/4/29 3:03
 * @Version 1.0
 **/
@Service("sunyurReturnOrderService")
public class SunyurReturnOrderServiceImpl implements SunyurReturnOrderService {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurReturnOrderServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurReturnOrderServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    /**
     * 退货单创建 链接
     */
    @Value("${sunyur.api.createReturnOrder}")
    private String createReturnOrder;

    /**
     * 查询退货单头 链接
     */
    @Value("${sunyur.api.getReturnOrder}")
    private String getReturnOrder;

    /**
     * 查询退货单行 链接
     */
    @Value("${sunyur.api.listReturnOrderItem}")
    private String listReturnOrderItem;

    /**
     * @return com.sunyur.tailor.channel.common.middle.base.ChannelResult<com.sunyur.tailor.channel.service.inner.returnorder.model.CreateReturnOrderModel>
     * @Author pengbing
     * @Description //退货单创建
     * @Date 14:01 2021/6/8
     * @Param [createReceiveOrderParam]
     **/
    @Override
    public ChannelResult<CreateReturnOrderModel> createReturnOrder(CreateReturnOrderParam createReturnOrderParam) throws Exception {
        LOGGER_INFO.info("sunyurReturnOrderService createReturnOrder param is {}", JSON.toJSONString(createReturnOrderParam));
        if (ObjectUtils.isEmpty(createReturnOrderParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "sunyurReturnOrderService createReturnOrder param is null");
        }
        // 调用主线接口
        SunyurResult<CreateReturnOrderModel> sunyurResult = sunyurClient.execPost(createReturnOrder, JSON.toJSONString(createReturnOrderParam), new TypeReference<SunyurResult<CreateReturnOrderModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelResult<ReturnOrderHeadModel> getReturnOrder(ReturnOrderHeadParam returnOrderHeadParam) {
        LOGGER_INFO.info("sunyurReturnOrderService getReturnOrder param is {}", JSON.toJSONString(returnOrderHeadParam));
        if (ObjectUtils.isEmpty(returnOrderHeadParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), "sunyurReturnOrderService getReturnOrder param is null");
        }
        // 调用主线接口
        SunyurResult<ReturnOrderHeadModel> sunyurResult = sunyurClient.execPost(getReturnOrder, JSON.toJSONString(returnOrderHeadParam), new TypeReference<SunyurResult<ReturnOrderHeadModel>>() {
        });
        // 主线报错
        if (!SunyurResultUtil.isSuccess(sunyurResult)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
        }
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    @Override
    public ChannelListResult<ReturnOrderItemModel> listReturnOrderItem(ReturnOrderItemParam returnOrderItemParam) {
        LOGGER_INFO.info("sunyurReturnOrderService getReturnOrder param is {}", JSON.toJSONString(returnOrderItemParam));
        if (ObjectUtils.isEmpty(returnOrderItemParam)) {
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), "sunyurReturnOrderService getReturnOrder param is null");
        }
        List<ReturnOrderItemModel> itemModelList = new ArrayList<>();
        Integer totalPage;
        Integer page = 0;
        returnOrderItemParam.setPageSize(100);
        //循环调用
        do {
            returnOrderItemParam.setPage(++page);
            // 调用主线接口
            SunyurListResult<ReturnOrderItemModel> sunyurResult = sunyurClient.execListPost(listReturnOrderItem, JSON.toJSONString(returnOrderItemParam), new TypeReference<SunyurListResult<ReturnOrderItemModel>>() {
            });
            // 主线报错
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
            totalPage = sunyurResult.getTotalPage();
            itemModelList.addAll(sunyurResult.getContent());
        } while (returnOrderItemParam.getPage() < totalPage);

        return ChannelListResultUtil.success(itemModelList);
    }

}
